﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zoo.Infrastructure.Data.Core;
using Zoo.Infrastructure.Caching;
using Zoo.Infrastructure.Events;
using Zoo.Cat.Domain.Entities.Polls;
using Zoo.Infrastructure;

namespace Zoo.Cat.Application.Polls {
    public class PollService : IPollService {
        #region Constants
        private const string POLLS_BY_ID_KEY = "Nop.polls.id-{0}";
        private const string POLLS_PATTERN_KEY = "Nop.polls.";
        #endregion

        #region Fields

        private readonly IRepository<Poll> _pollRepository;
        private readonly IRepository<PollAnswer> _pollAnswerRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;

        #endregion

        #region Ctor

        public PollService(IRepository<Poll> pollRepository,
            IRepository<PollAnswer> pollAnswerRepository,
            ICacheManager cacheManager, IEventPublisher eventPublisher) {
            _pollRepository = pollRepository;
            _pollAnswerRepository = pollAnswerRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
        }

        #endregion

        public Poll GetPollById(Guid pollId) {
            if (pollId.Equals(Guid.Empty))
                return null;

            string key = string.Format(POLLS_BY_ID_KEY, pollId);
            return _cacheManager.Get(key, () => {
                var poll = _pollRepository.GetById(pollId);
                return poll;
            });
        }

        public IPagedList<Poll> GetPolls(int pageIndex, int pageSize) {
            var query = _pollRepository.Table;

            query = query.OrderBy(p => p.DisplayOrder);

            var polls = new PagedList<Poll>(query, pageIndex, pageSize);
            return polls;
        }

        public void DeletePoll(Poll poll) {
            if (poll == null)
                throw new ArgumentNullException("poll");

            _pollRepository.Delete(poll);
            _cacheManager.RemoveByPattern(POLLS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityDeleted(poll);         
         
        }

        public void CreatePoll(Poll poll) {
            if (poll == null)
                throw new ArgumentNullException("poll");

            _pollRepository.Insert(poll);
            _cacheManager.RemoveByPattern(POLLS_PATTERN_KEY);

            // event notification
            _eventPublisher.EntityInserted(poll);
         
        }

        public void UpdatePoll(Poll poll) {
            if (poll == null)
                throw new ArgumentNullException("poll");

            _pollRepository.Update(poll);
            _cacheManager.RemoveByPattern(POLLS_PATTERN_KEY);

            // event notification
            _eventPublisher.EntityInserted(poll);
        }
    }
}
